<?php
/**
 * 设置类. 应用于随时关闭网站时使用
 * User: 1874
 * Date: 2019/8/5
 * Time: 16:27
 */

namespace app\adminplatform\controller;

use app\BaseController;
use think\facade\Session;

class Setting extends BaseController {
    //用户名
    protected $user = "1874";
    //密码
    protected $pwd = "ef4b5cd9b561d144a30ad66cac9605d019802a0c";
    //文件名
    protected $fileName = "website.txt";
    /*
     * 请求地址：/adminplatform/setting/administrator/user/1874/password/*******
     */
    public function administrator(){
        $userNumber = input('user');
        $password = sha1(md5(input('password')));

        if($userNumber == $this->user && $password == $this->pwd){
            Session::set('setWebsite', $password);
            return redirect((string) url('Setting/setWebsite',['open' => 0]));
        }else{
            return '请输入正确的指令';
        }
    }
    /*
     * 设置方法
     */
    public function setWebsite(){

        if(!Session::has('setWebsite')){
            return redirect((string) url('Setting/administrator'));
        }else{
            //session保存的密码 != $this->pwd 则跳转
            if(Session::get('setWebsite') != $this->pwd) return redirect((string) url('Setting/administrator'));
            //设置值
            $open = input('open');
            //文件是否存在,不存在则创建
            if(!file_exists($this->fileName)) file_put_contents($this->fileName, 0);
            //参数值对应操作
            if ($open == 1){
                //以写入形式读取文件
                $file = fopen($this->fileName, "w");
                fwrite($file, 1);
                fclose($file);
                return '已关闭网站';
            }elseif($open == 2){
                //以写入形式读取文件
                $file = fopen($this->fileName, "w");
                fwrite($file, 2);
                fclose($file);
                return '已开启网站';
            }else{
                return 'open=1 表示关闭网站，open=2 表示开启网站';
            }
        }
    }
    /*
     * 检测
     */
    /*protected static function checkWebsite(){
        header("Content-type: text/html; charset=utf-8");
        //获取请求参数
        $uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : null;
        //获取脚本路径
        $script = isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : null;
        //去掉 反斜杠
        $uri = trim($uri, '/');
        //脚部路径打散成数组并清除空值
        $fileUrl = array_filter(explode('/',$script));
        //如果没有uri
        if(empty($uri)) $uri = 'index/index/index';
        //程序是否在根目录 >1 则不在根目录
        if(count($fileUrl) > 1){
            //去掉该文件目录
            $uri = explode('/', $uri);
            unset($uri[0]);
            $uri = implode('/', $uri);
        }
        if (!empty($uri)){
            $url = strtolower(trim(str_replace($script,'',$uri),'/'));
            //setting 出现的位置
            $position = strpos($url, 'setting');
            //截取setting
            $url = $position === false ? $url : substr($url, $position, 7);
            if($url != 'setting'){
                //文件名
                $fileName = 'website.txt';
                if(!file_exists($fileName)){
                    exit('缺少关键性文件');
                }else{
                    //打开文件阅读
                    $websiteFile = fopen($fileName, "r");
                    //读取文件数据并判断
                    if(fread($websiteFile, 10) != 2) exit('404：所请求的页面不存在或已被删除！');
                }
            }
        }else{
            exit('403：所请求的地址不存在！');
        }
    }*/
}